package zhh.tangbao.machine.socket.handler.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import zhh.tangbao.commodity.constant.MachineRunningState;
import zhh.tangbao.machine.pojo.PreConnectionVerificationMachine;
import zhh.tangbao.machine.socket.handler.MachineSocketDisconnectHandler;
import zhh.tangbao.machine.util.MachineHolderUtil;
import zhh.tangbao.system.utils.ZhhDateUtil;
import zhh.tangbao.websocket.WebSocketSendMessageService;
import zhh.tangbao.websocket.WebSocketServiceType;
import zhh.tangbao.websocket.pojo.MachineRunning;
import zhh.tangbao.websocket.pojo.WebSocketMessageEntity;

import java.util.Date;

/**
 * <p>
 *     当机器心跳连接暂停后的处理策略
 * </p>
 * @author 粥灰灰
 * <p>2023/3/28</p>
 */

@Component
public class DefaultMachineSocketDisconnectHandler implements MachineSocketDisconnectHandler {


    @Autowired
    private WebSocketSendMessageService sendMessageService;

    @Autowired
    private ZhhDateUtil dateUtil;

    @Autowired
    private MachineHolderUtil machineHolderUtil;

    @Override
    public void disconnectWarning(PreConnectionVerificationMachine machine, Date date) {
        machineHolderUtil.remove(machine.getAcode());
        WebSocketMessageEntity me = new WebSocketMessageEntity();
        me.setMessageType(WebSocketServiceType.MESSAGE);
        me.setTitle("机器断线");
        me.setContent("机器编号: " + machine.getMachineId() + "在" + dateUtil.YMDHMS(date) + "时间断开连接");
        sendMessageService.sendMessageToAll("系统", me);
        me.setMessageType(WebSocketServiceType.MACHINE);
        me.setContent(new MachineRunning(machine.getMachineId(), MachineRunningState.CLOSE));
        sendMessageService.sendMessageToAll("系统", me);
    }

}
